#!/bin/sh

#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright (C) 2002-2010 Mark Wong & Open Source Development Labs, Inc.
#

usage()
{
	echo "usage: create-db -h"
	echo "usage: create-db [-p port]"
}

while getopts "d:hp:" OPT; do
	case ${OPT} in
	d)
		DBT5DBNAME=${OPTARG}
		;;
	h)
		usage
		exit 0
		;;
	p)
		PORT=${OPTARG}
		;;
	esac
done

if [ -z ${DBT5PGDATA} ]; then
	echo "DBT5PGDATA not defined."
	exit 1
fi

if [ "${DBT5DBNAME}" = "x" ]; then
	echo "DBT5DBNAME not defined."
	exit 1
fi

if [ ! "x${PORT}" = "x" ]; then
	PORTOPT="-p ${PORT}"
fi

# Create database
echo "Creating database..."
if [ -d ${DBT5PGDATA} ]; then
	echo "======================================="
	echo "PGData directory ${DBT5PGDATA} already exists"
	echo "Skipping initdb"
	echo "======================================="
else
	initdb -D ${DBT5PGDATA} --locale=C || exit 1
fi

if [ ! "x${PORT}" = "x" ]; then
	dbt5-pgsql-start-db -p "-c port=${PORT}"
else
	dbt5-pgsql-start-db
fi

# Give the database a few seconds to get going
sleep 4

createdb ${PORTOPT} -E SQL_ASCII ${DBT5DBNAME} || exit 1
# FIXME: Add a check for what version of PostgreSQL is being used and then
# determine whether we need to run createland got plpgsql.
#createlang ${PORTOPT} plpgsql ${DBT5DBNAME} || exit 1

exit 0
